package com.hsteach.service.patrol;

import com.hsteach.common.exception.ServiceException;
import com.hsteach.dao.domain.patrol.BaseConfig;
import com.hsteach.dao.mapper.BaseConfigMapper;
import com.hsteach.dao.mapper.RuleDescriptionMapper;
import com.hsteach.enums.BaseConfigEnum;
import com.hsteach.web.controller.patrol.dto.BaseConfigDTO;
import com.hsteach.web.controller.patrol.vo.BaseConfigVO;
import com.hsteach.web.controller.visit.vo.RuleDescriptionVO;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *
 * </p>
 *
 * @author mgb
 * @version BaseRuleService, java v1.0 2023-11-13 21:08 EXP $
 */
@Service
public class BaseRuleService {

    @Resource
    public BaseConfigMapper baseConfigMapper;
    @Resource
    public RuleDescriptionMapper ruleDescriptionMapper;

    /**
     * 获取所有配置信息
     *
     * @return
     */
    public List<BaseConfigVO> queryBaseConfig(String orgCode) {
        if (StringUtils.isEmpty(orgCode)){
            throw new ServiceException("orgCode不能为空");
        }

        List<BaseConfig> resultList = baseConfigMapper.queryBaseConfig(orgCode);
        List<String> configNameList = resultList.stream()
                .filter(Objects::nonNull)
                .filter(e->!StringUtils.isEmpty(e.getConfigName()))
                .map(BaseConfig::getConfigName).collect(Collectors.toList());
        if (!configNameList.contains("BASE_SCORE")) {
            BaseConfigDTO baseConfigDTO = new BaseConfigDTO();
            baseConfigDTO.setConfigName("BASE_SCORE");
            baseConfigDTO.setValue("1000");
            baseConfigDTO.setOrgCode(orgCode);
            baseConfigMapper.addBaseConfig(baseConfigDTO);
        }
        if (!configNameList.contains("EVALUATE_PERIOD")) {
            BaseConfigDTO baseConfigDTO = new BaseConfigDTO();
            baseConfigDTO.setConfigName("EVALUATE_PERIOD");
            baseConfigDTO.setValue("Week");
            baseConfigDTO.setOrgCode(orgCode);
            baseConfigMapper.addBaseConfig(baseConfigDTO);
        }
       resultList = baseConfigMapper.queryBaseConfig(orgCode);

        List<BaseConfigVO> baseConfigVOS = resultList.stream().map(baseConfig -> {
            BaseConfigVO baseConfigVO = new BaseConfigVO();
            baseConfigVO.setId(baseConfig.getId());
            baseConfigVO.setConfigName(baseConfig.getConfigName());
            if (BaseConfigEnum.EVALUATE_SCORE.getName().equals(baseConfig.getConfigName())
                    && StringUtils.isEmpty(baseConfig.getValue())) {
                baseConfigVO.setValue("Week");
            } else {
                baseConfigVO.setValue(baseConfig.getValue());
            }
            if(BaseConfigEnum.BASE_SCORE.getName().equals(baseConfig.getConfigName())
                    && StringUtils.isEmpty(baseConfig.getValue())){
                baseConfigVO.setValue("1000");
            }
            return baseConfigVO;
        }).collect(Collectors.toList());

        return baseConfigVOS;
    }

    public int updateBaseInfo(BaseConfigDTO baseConfigDTO) {
        if (StringUtils.isEmpty(baseConfigDTO.getOrgCode())) {
            throw new ServiceException("orgCode不能为空");
        }
        return baseConfigMapper.updateBaseInfo(baseConfigDTO);
    }

    /**
     * 新增规则说明
     *
     * @param ruleName
     * @param ruleDesc
     * @return
     */
    public int addRuleDesc(String ruleName, String ruleDesc, String orgCode) {
        //规则不存在则添加规则,存在则修改规则
        if (ruleDescriptionMapper.getRuleDescCountByRuleNameAndOrgCode(ruleName, orgCode)<=0) {
            return ruleDescriptionMapper.addRuleDesc(ruleName, ruleDesc, orgCode);
        }
        return ruleDescriptionMapper.updateRuleDescByRuleNameAndOrgCode(ruleName,ruleDesc,orgCode);
    }

    public RuleDescriptionVO getRuleDescById(String ruleName, String orgCode) {
        return ruleDescriptionMapper.getRuleDescById(ruleName,orgCode);
    }

    public int updateRuleDesc(RuleDescriptionVO ruleDescriptionVO) {

        return ruleDescriptionMapper.updateRuleDesc(ruleDescriptionVO);
    }

    public RuleDescriptionVO queryRuleDesc(String orgCode) {
        return ruleDescriptionMapper.queryRuleDesc(orgCode);
    }
}
